package com.usabb.controller;


import java.util.Arrays;

import com.usabb.api.dto.GuestCaseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.usabb.domain.GuestCaseDO;
import com.usabb.service.GuestCaseService;
import com.ifast.common.utils.Result;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * <pre>
 * 客人感言
 * </pre>
 * <small> 2019-08-19 18:03:26 | Aron</small>
 */
@Slf4j
@Controller
@RequestMapping("/usabb/guestCase")
public class GuestCaseController extends BaseController {
	@Autowired
	private GuestCaseService guestCaseService;
	
	@GetMapping()
	@RequiresPermissions("usabb:guestCase:guestCase")
	String GuestCase(){
	    return "usabb/guestCase/guestCase";
	}
	
	@ResponseBody
	@GetMapping("/list/{bizId}")
	@RequiresPermissions("usabb:guestCase:guestCase")
	public Result<Page<GuestCaseDO>> list(GuestCaseDO guestCaseDTO){
        Wrapper<GuestCaseDO> wrapper = new EntityWrapper<>(guestCaseDTO).orderBy("id", false);
        Page<GuestCaseDO> page = guestCaseService.selectPage(getPage(GuestCaseDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/{bizId}/add")
	@RequiresPermissions("usabb:guestCase:add")
	String add(@PathVariable("bizId") Long bizId, Model model) {
		model.addAttribute("bizId", bizId);
	    return "usabb/guestCase/add";
	}

	@GetMapping("/{bizId}/add_home_stay")
	@RequiresPermissions("usabb:guestCase:addHomeStay")
	String addHomeStay(@PathVariable("bizId") Long bizId, Model model) {
		model.addAttribute("bizId", bizId);
		return "usabb/guestCaseHomeStay/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("usabb:guestCase:edit")
	String edit(@PathVariable("id") Long id,Model model){
		GuestCaseDO guestCase = guestCaseService.selectById(id);
		model.addAttribute("guestCase", guestCase);
	    return "usabb/guestCase/edit";
	}
	
	/*@Log("添加客人感言")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("usabb:guestCase:add")
	public Result<String> save( GuestCaseDO guestCase){
		guestCaseService.insert(guestCase);
        return Result.ok();
	}
	@Log("修改客人感言")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("usabb:guestCase:edit")
	public Result<String>  update( GuestCaseDO guestCase){
		boolean update = guestCaseService.updateById(guestCase);
		return update ? Result.ok() : Result.fail();
	}*/

	@Log("添加客人感言")
	@ResponseBody
	@PostMapping("/save-with-image")
	@RequiresPermissions("usabb:guestCase:add")
	public Result<String> saveWithImage(@RequestParam Long bizId, @RequestParam String bizType,
										@RequestParam String nameIntro, @RequestParam String content,
										@RequestParam MultipartFile avatarFile) {
		GuestCaseDO guestCaseDO = new GuestCaseDO();
		guestCaseDO.setBizId(bizId);
		guestCaseDO.setBizType(bizType);
		guestCaseDO.setNameIntro(nameIntro);
		guestCaseDO.setContent(content);
		guestCaseService.addWithImage(guestCaseDO, avatarFile);

		return Result.ok();
	}

	@Log("修改客人感言 with image")
	@ResponseBody
	@PostMapping("/update-with-image")
	@RequiresPermissions("usabb:guestCase:update")
	public Result<String> updateWithImage(@RequestParam Long id,
										  @RequestParam Long bizId, @RequestParam String bizType,
										@RequestParam String nameIntro, @RequestParam String content,
										@RequestParam MultipartFile avatarFile) {
		GuestCaseDO guestCaseDO = new GuestCaseDO();
		guestCaseDO.setId(id);
		guestCaseDO.setBizId(bizId);
		guestCaseDO.setBizType(bizType);
		guestCaseDO.setNameIntro(nameIntro);
		guestCaseDO.setContent(content);
		guestCaseService.updateWithImage(guestCaseDO, avatarFile);

		return Result.ok();
	}

	
	@Log("删除客人感言")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("usabb:guestCase:remove")
	public Result<String>  remove( Long id){
		guestCaseService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除客人感言")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("usabb:guestCase:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		guestCaseService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
